# Contributor: lauren n. liberda <lauren@selfisekai.rocks>
# Maintainer: lauren n. liberda <lauren@selfisekai.rocks>
pkgname=element-desktop
pkgver=1.11.64
pkgrel=1
pkgdesc="Secure and independent communication, connected via Matrix"
url="https://element.io/"
arch="aarch64 x86_64" # same as electron
license="Apache-2.0"
depends="
	electron
	font-inconsolata
	font-inter
	font-nunito
	font-opensans
	font-twemoji
	"
makedepends="
	cargo
	electron-dev
	electron-tasje
	jq
	libsecret-dev
	nodejs
	npm
	python3
	py3-setuptools
	sqlcipher-dev
	yarn
	"
source="
	https://github.com/vector-im/element-desktop/archive/refs/tags/v$pkgver/element-desktop-$pkgver.tar.gz
	https://github.com/vector-im/element-web/archive/refs/tags/v$pkgver/element-web-$pkgver.tar.gz

	add-alpine-targets.patch
	use-system-headers.patch
	tasje-one-hak.patch
	tasje-no-fuses.patch
	use-system-fonts.patch.reactsdk
	use-system-fonts.patch.web

	element-desktop
	"
options="net !check" # broken

# secfixes:
#   1.11.30-r0:
#     - CVE-2023-30609
#   1.11.26-r0:
#     - CVE-2023-28103
#     - CVE-2023-28427
#   1.11.7-r0:
#     - CVE-2022-39249
#     - CVE-2022-39250
#     - CVE-2022-39251
#     - CVE-2022-39236
#   1.11.4-r0:
#     - CVE-2022-36059
#     - CVE-2022-36060

# used by buildscripts (at least web's webpack)
export VERSION=$pkgver

export CARGO_PROFILE_RELEASE_OPT_LEVEL=2
export CARGO_PROFILE_RELEASE_STRIP="symbols"
export NODE_OPTIONS="--openssl-legacy-provider"

prepare() {
	default_prepare

	msg "Applying more patches"
	for x in $source; do
		case "$x" in
		*.patch.web)
			msg "$x"
			patch -p1 -i "$srcdir"/$x -d "$srcdir"/element-web-$pkgver
			;;
		esac
	done

	(
		cd "$srcdir"/element-web-$pkgver

		msg "Fetch element-web dependencies"
		yarn install --frozen-lockfile --ignore-scripts
		jq '.show_labs_settings = true' < config.sample.json > config.json

		msg "Applying more patches"
		for x in $source; do
			case "$x" in
			*.patch.reactsdk)
				msg "$x"
				patch -p1 -i "$srcdir"/$x -d "$srcdir"/element-web-$pkgver/node_modules/matrix-react-sdk
				;;
			esac
		done

		rm -rf node_modules/matrix-react-sdk/res/fonts/
	)

	ln -s "$srcdir"/element-web-$pkgver/webapp webapp

	msg "Fetch element-desktop dependencies"
	yarn install --frozen-lockfile --ignore-scripts
}

build() {
	(
		cd "$srcdir"/element-web-$pkgver

		msg "Build element-web"
		NODE_ENV=production yarn build
	)

	msg "Build element-desktop"

	yarn asar-webapp

	# add "optional" native dependencies
	# hak stands for hack
	yarn run hak --target "$(uname -m)-alpine-linux-musl"

	# stripping in build because it gets into asar
	strip node_modules/keytar/build/Release/keytar.node

	yarn build:ts

	yarn build:res

	yarn install --frozen-lockfile --ignore-scripts --production

	tasje -c electron-builder.js pack
}

check() {
	(
		cd "$srcdir"/element-web-$pkgver

		yarn test
	)
}

package() {
	local resources="dist/resources"

	install -Dm644 $resources/app.asar "$pkgdir"/usr/lib/element-desktop/app.asar
	install -Dm644 webapp.asar "$pkgdir"/usr/lib/element-desktop/webapp.asar

	cp -r $resources/app.asar.unpacked "$pkgdir"/usr/lib/element-desktop/app.asar.unpacked

	install -Dm644 $resources/img/element.png "$pkgdir"/usr/lib/element-desktop/img/element.png

	install -Dm755 "$srcdir"/$pkgname "$pkgdir"/usr/bin/$pkgname

	install -Dm644 dist/$pkgname.desktop "$pkgdir"/usr/share/applications/$pkgname.desktop
	while read -r size; do
		install -Dm644 dist/icons/$size.png "$pkgdir"/usr/share/icons/hicolor/$size/apps/$pkgname.png
	done < dist/icons/size-list
}

sha512sums="
3627b7ce207d10f73e91bca5f4bf5bd71262fa22eab678788f25fd6b89b9baaf55c41b642d855833196b1ed02dde8c73095ce5056211375b477b7c6d7ac4a4c4  element-desktop-1.11.64.tar.gz
bc74ff9330c2972e22fdcd54a9a552c9dacc792874d7d7a30929b1f973f638fa4a88827bf5bc0268761175c21077ab5482475d012512ec480512b1eb15ddbc96  element-web-1.11.64.tar.gz
4747893ed3e43d3074e9afe1cdd668a6be0de073d439205fe8c38c5e0f4091cc76e3cd15d98818bea5139add29501d8d07e83c58e9da230a4ce5bb538d388f80  add-alpine-targets.patch
83f5e5fb197bc78e13dbb86dfcf2ef39c25ad9b54b72a911ad33fcd4a9725b6612e240edae030154b1512149408541b19f267bf517314447f1a2c6de6a45469e  use-system-headers.patch
e4f74e9b5eb6f6e8ea893925e3ad4fca2bbf7d6ba64e82e230f154d1c846668fe66fdd44875b1c8ac0281e7cad6a12e462ab765281e997a16616ccf855d82aa4  tasje-one-hak.patch
9af00259f5e236f70a308f315d0c9116a68e1223d057a219344a8c72ec9b5694b92e3a1cb030c918a5be8c112dd1a5920b65b2b1eaf2b5b61fcb0d743d9a94b8  tasje-no-fuses.patch
31e3bde9ac13d711af3b57768eea5f60013f93ac35ec49648a66da8b4652e5049c991bfadfbe373814f237c700b95f5b9d1ed0c8f7cf8e4ec489a44b17dbfad5  use-system-fonts.patch.reactsdk
7dd96eb00dfc13e30d2274650a79aafce1bae3572ffcb4a5ce6a0ee6e4bc73cc8d6c0845d2dea930006f0fdd0ff313cd7615e6de58c95e674cb41dc978377f1d  use-system-fonts.patch.web
afc588311dc3b566a754e3e7fe6b37b99a06d47b8bbce0ed9acca8ef308fdab0bd1d41b406199e5cbdd86bdce695ff847cd8668857a235cbdc292ad8b899c063  element-desktop
"
